<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.company.ems.model.User" %>
<%@ page import="java.util.List" %>
<%@ page import="com.company.ems.model.Employee" %>
<%
    User user = (User) session.getAttribute("user");
    if (user == null) {
        response.sendRedirect("../login.jsp");
        return;
    }
    
    List<Employee> employees = (List<Employee>) request.getAttribute("employees");
    String searchName = (String) request.getAttribute("name");
    String searchDepartment = (String) request.getAttribute("department");
    String searchPosition = (String) request.getAttribute("position");
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>员工管理系统 - 员工列表</title>
    <link rel="stylesheet" href="../css/style.css">
</head>
<body>
    <div class="container">
        <!-- 头部 -->
        <div class="header">
            <h1>员工管理系统</h1>
            <div class="user-info">
                <span>欢迎，<%= user.getUsername() %></span>
                <a href="../logout" class="btn btn-secondary">退出登录</a>
            </div>
        </div>
        
        <!-- 导航栏 -->
        <div class="nav">
            <a href="../dashboard">仪表板</a>
            <a href="./" class="active">员工管理</a>
            <a href="./add">添加员工</a>
        </div>
        
        <!-- 内容区域 -->
        <div class="content">
            <!-- 搜索表单 -->
            <div class="search-form">
                <form action="./search" method="get" class="form-row">
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" id="name" name="name" class="form-control" 
                               value="<%= searchName != null ? searchName : "" %>" placeholder="输入姓名搜索">
                    </div>
                    <div class="form-group">
                        <label for="department">部门</label>
                        <select id="department" name="department" class="form-control">
                            <option value="">全部部门</option>
                            <option value="技术部" <%= "技术部".equals(searchDepartment) ? "selected" : "" %>>技术部</option>
                            <option value="市场部" <%= "市场部".equals(searchDepartment) ? "selected" : "" %>>市场部</option>
                            <option value="人事部" <%= "人事部".equals(searchDepartment) ? "selected" : "" %>>人事部</option>
                            <option value="财务部" <%= "财务部".equals(searchDepartment) ? "selected" : "" %>>财务部</option>
                            <option value="行政部" <%= "行政部".equals(searchDepartment) ? "selected" : "" %>>行政部</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="position">职位</label>
                        <select id="position" name="position" class="form-control">
                            <option value="">全部职位</option>
                            <option value="软件工程师" <%= "软件工程师".equals(searchPosition) ? "selected" : "" %>>软件工程师</option>
                            <option value="市场专员" <%= "市场专员".equals(searchPosition) ? "selected" : "" %>>市场专员</option>
                            <option value="HR经理" <%= "HR经理".equals(searchPosition) ? "selected" : "" %>>HR经理</option>
                            <option value="财务主管" <%= "财务主管".equals(searchPosition) ? "selected" : "" %>>财务主管</option>
                            <option value="行政助理" <%= "行政助理".equals(searchPosition) ? "selected" : "" %>>行政助理</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">搜索</button>
                        <a href="./" class="btn btn-secondary">重置</a>
                    </div>
                </form>
            </div>
            
            <!-- 操作按钮 -->
            <div style="margin-bottom: 20px;">
                <a href="./add" class="btn btn-success">添加新员工</a>
            </div>
            
            <!-- 员工列表 -->
            <div class="card">
                <div class="card-header">
                    员工列表
                    <% if (employees != null) { %>
                        <span style="float: right; font-weight: normal; color: #6c757d;">
                            共 <%= employees.size() %> 条记录
                        </span>
                    <% } %>
                </div>
                <div class="card-body">
                    <% if (employees != null && !employees.isEmpty()) { %>
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>姓名</th>
                                    <th>部门</th>
                                    <th>职位</th>
                                    <th>薪资</th>
                                    <th>邮箱</th>
                                    <th>电话</th>
                                    <th>入职时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <% for (Employee emp : employees) { %>
                                    <tr>
                                        <td><%= emp.getId() %></td>
                                        <td><%= emp.getName() %></td>
                                        <td><%= emp.getDepartment() %></td>
                                        <td><%= emp.getPosition() %></td>
                                        <td>¥<%= String.format("%.2f", emp.getSalary()) %></td>
                                        <td><%= emp.getEmail() != null ? emp.getEmail() : "-" %></td>
                                        <td><%= emp.getPhone() != null ? emp.getPhone() : "-" %></td>
                                        <td><%= new java.text.SimpleDateFormat("yyyy-MM-dd").format(emp.getHireDate()) %></td>
                                        <td>
                                            <!-- 修复链接路径问题 -->
                                            <a href="./view?id=<%= emp.getId() %>" class="btn btn-primary">查看</a>
                                            <a href="./edit?id=<%= emp.getId() %>" class="btn btn-warning">编辑</a>
                                            <a href="javascript:void(0)" onclick="confirmDelete(<%= emp.getId() %>, '<%= emp.getName() %>')" 
                                               class="btn btn-danger">删除</a>
                                        </td>
                                    </tr>
                                <% } %>
                            </tbody>
                        </table>
                    <% } else { %>
                        <div style="text-align: center; padding: 40px; color: #6c757d;">
                            <p style="font-size: 16px; margin-bottom: 15px;">暂无员工数据</p>
                            <a href="./add" class="btn btn-primary">添加第一个员工</a>
                        </div>
                    <% } %>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        function confirmDelete(id, name) {
            if (confirm('确定要删除员工 "' + name + '" 吗？此操作不可恢复。')) {
                window.location.href = './delete?id=' + id;
            }
        }
    </script>
</body>
</html>